package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdminAgentInfoMapper;
import com.ruoyi.system.domain.AdminAgentInfo;
import com.ruoyi.system.service.IAdminAgentInfoService;

/**
 * 代理信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-11
 */
@Service
public class AdminAgentInfoServiceImpl implements IAdminAgentInfoService 
{
    @Autowired
    private AdminAgentInfoMapper adminAgentInfoMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询代理信息
     * 
     * @param id 代理信息ID
     * @return 代理信息
     */
    @Override
    public AdminAgentInfo selectAdminAgentInfoById(Long id)
    {
        return adminAgentInfoMapper.selectAdminAgentInfoById(id);
    }

    @Override
    public AdminAgentInfo selectAdminAgentInfoByRoom(String room) {
        return adminAgentInfoMapper.selectAdminAgentInfoByRoom(room);
    }

    @Override
    public AdminAgentInfo selectAdminAgentInfoByCommand(String command) {
        return adminAgentInfoMapper.selectAdminAgentInfoByCommand(command);
    }

    @Override
    public AdminAgentInfo selectAdminAgentInfoByCommandAndAgentId(AdminAgentInfo adminAgentInfo) {
        return adminAgentInfoMapper.selectAdminAgentInfoByCommandAndAgentId(adminAgentInfo);
    }

    /**
     * 查询代理信息列表
     * 
     * @param adminAgentInfo 代理信息
     * @return 代理信息
     */
    @Override
    public List<AdminAgentInfo> selectAdminAgentInfoList(AdminAgentInfo adminAgentInfo)
    {
        return adminAgentInfoMapper.selectAdminAgentInfoList(adminAgentInfo);
    }

    @Override
    public List<AdminAgentInfo> selectAdminAgentInfoListForAdmin(AdminAgentInfo adminAgentInfo) {
        return adminAgentInfoMapper.selectAdminAgentInfoListForAdmin(adminAgentInfo);
    }

    /**
     * 新增代理信息
     * 
     * @param adminAgentInfo 代理信息
     * @return 结果
     */
    @Override
    public int insertAdminAgentInfo(AdminAgentInfo adminAgentInfo)
    {
        Integer count = adminAgentInfoMapper.selectAdminAgentInfoCountByAgentName(adminAgentInfo.getAgentName());
        AdminAgentInfo adminAgentInfo1 = adminAgentInfoMapper.selectAdminAgentInfoByCommand(adminAgentInfo.getCommand());
        if (adminAgentInfo1==null) {
            if (count==0) {
                if (!adminAgentInfo.getAgentName().equals("admin")) {
                    adminAgentInfo.setCreateTime(DateUtils.getNowDate());
                    String room = generateRoom();
                    adminAgentInfo.setRoom(room);
                    adminAgentInfoMapper.insertAdminAgentInfo(adminAgentInfo);
//                    String userName = generateUserName();
                    SysUser user = new SysUser();
                    user.setNickName(adminAgentInfo.getAgentName());
                    user.setStatus(new String("0"));
                    user.setCreateTime(DateUtils.getNowDate());
                    user.setUserName(room);
                    user.setRoom(room);
                    user.setCreateBy(SecurityUtils.getUsername());
                    user.setPassword(SecurityUtils.encryptPassword("123456"));
                    userMapper.insertUser(user);
                    ArrayList<SysUserRole> sysUserRoles = new ArrayList<>();
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(2L);
                    sysUserRole.setUserId(user.getUserId());
                    sysUserRoles.add(sysUserRole);
                    userRoleMapper.batchUserRole(sysUserRoles);
                    return 1;
                }else {
                    adminAgentInfoMapper.insertAdminAgentInfo(adminAgentInfo);
                    return 1;
                }
            }else{
                return 0;
            }
        }else{
            return 2;
        }
    }

    public  String generateRoom(){
        String room="";
        Boolean isRepeat=true;
        do{
            Integer i = (int) ((Math.random()*9+1)*100000);
            room = i.toString();
            Integer count = adminAgentInfoMapper.selectAdminAgentInfoCountByRoom(room);
            if (count==0) {
                isRepeat=false;
            }
        }while(isRepeat);
        return room;
    }

    public String generateUserName(){
        String userName="";
        Boolean isRepeat=true;
        do{
            String str1="agent";
            int i = (int) ((Math.random() * 9 + 1) * 100);
            userName = str1 + i;
            Integer count = userMapper.selectUserByUserNameCount(userName);
            if (count==0) {
                isRepeat=false;
            }
        }while(isRepeat);
        return userName;
    }

    public static void main(String[] args) {
        System.out.println((int)((Math.random()*9+1)*100000));
    }

    /**
     * 修改代理信息
     * 
     * @param adminAgentInfo 代理信息
     * @return 结果
     */
    @Override
    public int updateAdminAgentInfo(AdminAgentInfo adminAgentInfo)
    {
        adminAgentInfo.setUpdateTime(DateUtils.getNowDate());
        return adminAgentInfoMapper.updateAdminAgentInfo(adminAgentInfo);
    }

    /**
     * 批量删除代理信息
     * 
     * @param ids 需要删除的代理信息ID
     * @return 结果
     */
    @Override
    public int deleteAdminAgentInfoByIds(Long[] ids)
    {
        return adminAgentInfoMapper.deleteAdminAgentInfoByIds(ids);
    }

    /**
     * 删除代理信息信息
     * 
     * @param id 代理信息ID
     * @return 结果
     */
    @Override
    public int deleteAdminAgentInfoById(Long id)
    {
        return adminAgentInfoMapper.deleteAdminAgentInfoById(id);
    }
}
